#include "singcustomwave.h"

#include <shareAlg/arrayOper/arrayoperation.h>

SingCustomWave::SingCustomWave(QCustomPlot *plot)
{
    this->customPlot=plot;
    this->SampleRate=250;
    this->ShowDuration=8;
    SAMPLE_INTERVAL=1.0/(double)SampleRate;
    m_currentTime=0;
    initWave();
}

void SingCustomWave::initWave()
{
    QFont font = QFont();
    QColor TextColor(QColor(255, 255, 255));
    customPlot->legend->setFont(font);
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::red));
    customPlot->xAxis->setLabelFont(font);
    customPlot->yAxis->setLabelFont(font);
    customPlot->xAxis->setTickLabelFont(font);
    customPlot->yAxis->setTickLabelFont(font);
    customPlot->xAxis->setLabel("时间 (秒)");
    customPlot->yAxis->setLabel("电压值");
    customPlot->xAxis->setRange(0, ShowDuration);
    customPlot->yAxis->setRange(-0.3, 0.3);
    //设置坐标颜色/坐标名称颜色
    customPlot->yAxis->setLabelColor(TextColor);
    customPlot->xAxis->setLabelColor(TextColor);
    customPlot->xAxis->setTickLabelColor(TextColor);
    customPlot->yAxis->setTickLabelColor(TextColor);
    // plot->xAxis->setBasePen(QPen(TextColor, TextWidth));
    // plot->yAxis->setBasePen(QPen(TextColor, TextWidth));
    // plot->xAxis->setTickPen(QPen(TextColor, TextWidth));
    // plot->yAxis->setTickPen(QPen(TextColor, TextWidth));
    // plot->xAxis->setSubTickPen(QPen(TextColor, TextWidth));
    // plot->yAxis->setSubTickPen(QPen(TextColor, TextWidth));

    //设置画布背景色
    QLinearGradient plotGradient;
    plotGradient.setStart(0, 0);
    plotGradient.setFinalStop(0, 350);
    plotGradient.setColorAt(0, QColor(80, 80, 80));
    plotGradient.setColorAt(1, QColor(50, 50, 50));
    customPlot->setBackground(plotGradient);

    //设置坐标背景色
    QLinearGradient axisRectGradient;
    axisRectGradient.setStart(0, 0);
    axisRectGradient.setFinalStop(0, 350);
    axisRectGradient.setColorAt(0, QColor(80, 80, 80));
    axisRectGradient.setColorAt(1, QColor(30, 30, 30));
    customPlot->axisRect()->setBackground(axisRectGradient);

    //设置图例提示位置及背景色
    customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
    customPlot->legend->setBrush(QColor(255, 255, 255, 200));
    customPlot->replot();
}
void SingCustomWave::SetData(QVector<double>data)
{
    // 添加新数据
    for (double value : data) {
        if (m_yData.size()  >= 2000) {
            m_yData.removeFirst();
            m_xData.removeFirst();
        }
        m_yData.append(value);
        m_xData.append(m_currentTime);
        m_currentTime += SAMPLE_INTERVAL;
        SampleNum++;
        if(SampleNum%SampleRate==0)
        {
            double minVal, maxVal;
            ArrayOperation::instance().maximum(m_yData,minVal,maxVal);
            customPlot->yAxis->setRange(minVal, maxVal);
        }
    }

    // 更新X轴范围
    double windowEnd = qMax(m_currentTime, (double)ShowDuration);

    customPlot->xAxis->setRange(windowEnd - ShowDuration, windowEnd);

    customPlot->graph(0)->setData(m_xData, m_yData);
    customPlot->replot();

}
